package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.MeetingUserSignature;
import com.ruoyi.system.mapper.MeetingUserSignatureMapper;
import com.ruoyi.system.service.IMeetingUserSignatureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.MeetingSupplementSignatureMapper;
import com.ruoyi.system.domain.MeetingSupplementSignature;
import com.ruoyi.system.service.IMeetingSupplementSignatureService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

/**
 * 会议补签审批Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-11
 */
@Service
public class MeetingSupplementSignatureServiceImpl implements IMeetingSupplementSignatureService
{
    @Autowired
    private MeetingSupplementSignatureMapper meetingSupplementSignatureMapper;
    @Autowired
    private IMeetingUserSignatureService meetingUserSignatureService;
    @Autowired
    private MeetingUserSignatureMapper meetingUserSignatureMapper;

    /**
     * 查询会议补签审批
     *
     * @param id 会议补签审批主键
     * @return 会议补签审批
     */
    @Override
    public MeetingSupplementSignature selectMeetingSupplementSignatureById(String id)
    {
        return meetingSupplementSignatureMapper.selectMeetingSupplementSignatureById(id);
    }

    /**
     * 查询会议补签审批列表
     *
     * @param meetingSupplementSignature 会议补签审批
     * @return 会议补签审批
     */
    @Override
    public List<MeetingSupplementSignature> selectMeetingSupplementSignatureList(MeetingSupplementSignature meetingSupplementSignature)
    {
        return meetingSupplementSignatureMapper.selectMeetingSupplementSignatureList(meetingSupplementSignature);
    }

    /**
     * 新增会议补签审批
     *
     * @param meetingSupplementSignature 会议补签审批
     * @return 结果
     */
    @Override
    public int insertMeetingSupplementSignature(MeetingSupplementSignature meetingSupplementSignature)
    {
        String date = DateUtils.dateTimeNow();
        String id = date + IdUtils.fastSimpleEnglishUUID(5);
        meetingSupplementSignature.setId(id);
        meetingSupplementSignature.setCreateTime(DateUtils.getNowDate());
        return meetingSupplementSignatureMapper.insertMeetingSupplementSignature(meetingSupplementSignature);
    }

    /**
     * 修改会议补签审批
     *
     * @param meetingSupplementSignature 会议补签审批
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateMeetingSupplementSignature(MeetingSupplementSignature meetingSupplementSignature)
    {
        meetingSupplementSignature.setUpdateTime(DateUtils.getNowDate());
        int i = meetingSupplementSignatureMapper.updateMeetingSupplementSignature(meetingSupplementSignature);
        MeetingSupplementSignature supplementSignature = meetingSupplementSignatureMapper.selectMeetingSupplementSignatureById(meetingSupplementSignature.getId());
        if(!ObjectUtils.isEmpty(supplementSignature)){
            String status = meetingSupplementSignature.getStatus();
            MeetingUserSignature meetingUserSignature = new MeetingUserSignature();
            meetingUserSignature.setUserId(supplementSignature.getUserId());
            meetingUserSignature.setMeetingId(supplementSignature.getMeetingId());
            MeetingUserSignature signature = meetingUserSignatureMapper.selectMeetingUserSignature(meetingUserSignature);
            if("1".equals( status)){
                //审批通过，修改签到信息为补签
                signature.setApprovalNum(supplementSignature.getId());
                signature.setStatus("4");
                meetingUserSignatureService.updateMeetingUserSignature(signature);
            }else if("2".equals( status)){
                //（通过之后）审批撤销，修改签到信息为迟到
                signature.setStatus("3");
                meetingUserSignatureService.updateMeetingUserSignature(signature);
            }
        }
        return i;
    }

    /**
     * 删除会议补签审批信息
     *
     * @param id 会议补签审批主键
     * @return 结果
     */
    @Override
    public int deleteMeetingSupplementSignatureById(String id)
    {
        return meetingSupplementSignatureMapper.deleteMeetingSupplementSignatureById(id);
    }
}
